<?php

namespace model;

use common\Model;

class applinks extends Model
{
    private $DB_TABLE = "applink";
    private $DB_TABLE_CATE = "applink_cate";
    private $DB;
    function __construct()
    {
        $this->DB = parent::db();
    }

    //关联查找一条数据
    public function findData()
    {
        return $this->DB->table($this->DB_TABLE)->where(['id' => $_GET['id']])->find();
    }

    //获取数据并分页
    public function jsonPageSelect()
    {
        //根据标题搜索内容
        if (!empty($_GET['name']))
            $where['a.name LIKE'] = '%' . $_GET['name'] . '%';

        if (isset($_GET['searchcid']))
            $newCid = $_GET['searchcid'] > 0 || $_GET['searchcid'] == 0 ? $_GET['searchcid'] : $_GET['cid'];
        else
            $newCid = $_GET['cid'];

        if ($newCid > 0)
            $where['a.cid'] = $newCid;

        $p = intval($_GET['page'] ?? 0) ?: 1;
        $num = intval($_GET['limit'] ?? 0) ?: 15;
        $page = ['num' => $num, 'p' => $p, 'return' => true];

        $field = 'b.id as cateid , b.name as catename,a.id,a.cid,a.name,a.url,a.ico,a.sort,a.status';
        $join = 'LEFT JOIN applink_cate b ON a.cid=b.id';
        return [
            'data' => $this->DB->table($this->DB_TABLE . ' a')->field($field)->join($join)->where($where)->Page($page)->order('a.id DESC')->select(),
            'page' => $this->DB->getPage()
        ];
    }

    private function PostData()
    {
        return [
            'cid' => $_POST['cid'],
            'name' =>  VarFilter($_POST['name']),
            'url' =>  VarFilter($_POST['url']),
            'ico' => $_POST['ico'],
            'sort' => $_POST['sort'],
            'status' => $_POST['status'],
        ];
    }

    //添加数据
    public function insertData()
    {
        if (empty($_POST['name']))
            return ['status' => 0, 'msg' => '请填写必要数据'];
        $re_key = $this->DB->table($this->DB_TABLE)->insert($this->PostData());
        if ($re_key) {
            //写入日志
            SqlLog("添加应用链接 ID:" . $re_key);
            CleanDbCache($this->DB_TABLE);
            return ['key' => $re_key, 'status' => 1, 'msg' => '添加成功'];
        } else {
            return ['status' => 0, 'msg' => '数据错误！添加失败'];
        }
    }
    //更新数据
    public function updateData()
    {
        if (empty($_POST['name']))
            return ['status' => 0, 'msg' => '请填写必要数据'];
        $where['id'] = $_POST['id'];
        $re_key = $this->DB->table($this->DB_TABLE)->Where($where)->Update($this->PostData());
        if ($re_key) {
            SqlLog("修改应用链接 ID:" . $_POST['id']);
            CleanDbCache($this->DB_TABLE);
            return ['status' => 1, 'msg' => '修改成功!!'];
        } else {
            return ['status' => 0, 'msg' => '数据没有变化'];
        }
    }

    //更新数据
    public function updateSort()
    {
        if ($this->DB->table($this->DB_TABLE)->Where(['id' => $_GET['id']])->Update(['sort' => $_POST['sort']])) {
            SqlLog("修改应用链接排序 ID:" . $_GET['id']);
            CleanDbCache($this->DB_TABLE);
            return  ['status' => 1, 'msg' => '修改成功'];
        } else {
            return  ['status' => 0, 'msg' => '未修改'];
        }
    }

    //设置状态
    public function switchStatus()
    {
        SqlLog("切换应用链接状态 ID:" . $_POST['id']);
        return $this->DB->table($this->DB_TABLE)->where(['id' => $_POST['id']])->Update(['status' => $_POST['status']]);
    }

    //删除一条数据
    public function deleteOneData()
    {
        SqlLog("删除应用链接 ID:" . $_POST['id']);
        return $this->DB->table($this->DB_TABLE)->where(['id' => $_POST['id']])->Delete();
    }



    /******
     * 应用分类
     * ********/

    //关联查找一条数据
    public function findDataCate()
    {
        return $this->DB->table($this->DB_TABLE_CATE)->where(['id' => $_GET['id']])->find();
    }

    //获取栏目列表
    public function SelectDataCate()
    {
        return $this->DB->table($this->DB_TABLE_CATE)->select();
    }

    //获取数据并分页
    public function jsonPageSelectCate()
    {
        $p = intval($_GET['page'] ?? 0) ?: 1;
        $num = intval($_GET['limit'] ?? 0) ?: 15;

        $page = ['num' => $num, 'p' => $p, 'return' => true];
        return [
            'data' => $this->DB->table($this->DB_TABLE_CATE)->Page($page)->select(),
            'page' => $this->DB->getPage()
        ];
    }

    //添加数据
    public function insertDataCate()
    {
        if (empty($_POST['name']))
            return ['status' => 0, 'msg' => '请填写必要数据'];
        $data = [
            'name' => $_POST['name'],
        ];
        $re_key = $this->DB->table($this->DB_TABLE_CATE)->insert($data);
        if ($re_key) {
            SqlLog("添加应用链接分类 ID:" . $re_key);
            CleanDbCache($this->DB_TABLE_CATE);
            return ['key' => $re_key, 'status' => 1, 'msg' => '添加成功'];
        } else {
            return ['status' => 0, 'msg' => '数据错误！添加失败'];
        }
    }
    //更新数据
    public function updateDataCate()
    {
        if (empty($_POST['name']))
            return ['status' => 0, 'msg' => '请填写必要数据'];
        $data = [
            'name' => $_POST['name'],
        ];
        $re_key = $this->DB->table($this->DB_TABLE_CATE)->Where(['id' => $_POST['id']])->Update($data);
        if ($re_key > 0) {
            SqlLog("修改应用链接分类 ID:" . $_POST['id']);
            CleanDbCache($this->DB_TABLE_CATE);
            return ['status' => 1, 'msg' => '修改成功!!'];
        } else {
            return ['status' => 0, 'msg' => '数据没有变化'];
        }
    }


    public function deleteOneDataCate()
    {
        $result = $this->DB->table($this->DB_TABLE_CATE)->where(['id' => $_POST['id']])->Delete();
        SqlLog("删除应用链接分类 ID:" . $_POST['id']);
        return $result;
    }
}
